package versions

import (
	"encoding/json"
	"lib/es"
	"lib/utils"
	"log"
	"net/http"
)

func Handler(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodGet {
		w.WriteHeader(http.StatusMethodNotAllowed)
		return
	}

	from := 0
	size := 1000
	name := utils.GetObjectName(r)

	for {
		metas, e := es.SearchAllVersion(name, from, size)
		if e != nil {
			log.Println("Search all version failed: ", e)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		for i := range metas {
			b, _ := json.Marshal(metas[i])
			w.Write(b)
			w.Write([]byte("\n"))
		}
		if len(metas) != size {
			return
		}
		from += size
	}
}
